﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Data.SqlClient;
using System.Threading;
public partial class Admin_LoaiPhim : System.Web.UI.Page
{
    
    Theloaiphim TLP = new Theloaiphim();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {
            if (Session["user"] != null)
            {
                GridView1.DataSource = TLP.TLPhim();
                GridView1.DataBind();
            }
            else
                Response.Redirect("DangNhap.aspx");
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {//tim
       
        GridView1.DataSource = TLP.TimTLP(TextBox1.Text);
        GridView1.DataBind();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("ThemTheLoaiPhim.aspx");
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        GridView1.DataSource = TLP.TLPhim();
        GridView1.DataBind();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        GridView1.DataSource = TLP.TLPhim();
        GridView1.DataBind();

    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int  matl=int.Parse(GridView1.Rows[e.RowIndex].Cells[1].Text);
        TextBox txtTen = (TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0];
        string ten = txtTen.Text;

        TLP.SuaTLP(matl, ten);
        GridView1.EditIndex = -1;
        GridView1.DataSource = TLP.TLPhim();
        GridView1.DataBind();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        
       int  ma = int.Parse(GridView1.Rows[e.RowIndex].Cells[1].Text);
     
       try
       {

           TLP.XoaTLP(ma);
           Label1.Text = "The Loai da duoc xoa";
       }
       catch (Exception ex)
       {

           Label1.Text = ex.Message;
       }
       
          
        GridView1.DataSource = TLP.TLPhim();
        GridView1.DataBind();
       
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataSource = TLP.TLPhim();
        GridView1.DataBind();

    }
    protected void Btnxoa_Click(object sender, EventArgs e)
    {
        Thread.Sleep(2000);
        try
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox chb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("cb");

                if (chb.Checked == true)
                {
                    int ma = Convert.ToInt32(GridView1.Rows[i].Cells[1].Text);
                   TLP.XoaTLP(ma);

                }
            }
        }
        catch (Exception ex)
        {
            Label1.Text = ex.Message;

        }

        GridView1.DataSource = TLP.TLPhim();
        GridView1.DataBind();
    }
}
